const net = require('net');

module.exports = (server) => {
  server.on('request', (req, res) => {
    let auth = req.headers['proxy-authorization'];
    auth = auth && auth.trim().split(/\s+/)[1];
    if (!auth) {
      return res.end();
    }
    auth = `${Buffer.from(auth, 'base64')}`.split(':');
    const host = net.isIP(auth[0]) ? auth[0] : '127.0.0.1';
    const port = auth[1] > 0 ? auth[1] : 5000;
    res.end(`* socks://${host}:${port}`);
  });
};
